package com.zhu.utils;


import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

/**
 * 异步IO 线程工具类 单例
 */
public class ZhuThreadPoolUtil {

    private static ThreadPoolExecutor threadPoolExecutor;

    //方法私有
    private ZhuThreadPoolUtil(){

    }

    public static ThreadPoolExecutor getThreadPoolExecutor(){

        if(threadPoolExecutor == null) {  //饿汉式 双重校验 提高效率 所有线程不都经过锁
            synchronized (ThreadPoolExecutor.class) {
                if (threadPoolExecutor == null) {
                    threadPoolExecutor = new ThreadPoolExecutor(
                            4,    // bingxing 1000 kakfa 4 和kafka partition 大于 5 6 压测
                            20,
                            100,
                            TimeUnit.SECONDS,
                            new LinkedBlockingQueue<>()
                    );   //核心线程  最大线程 keepLiveTime 空闲销毁时间 workQueue 工作队列
                }
            }
        }
        return threadPoolExecutor;
    }
}
